
package com.primecodegen.common.GeneratedTargets;

import com.primecodegen.common.MainProject.MainProjectDAO;
import com.primecodegen.common.MainProject.MainProjectTable;
import com.primecodegen.util.*;
import com.primecodegen.common.base.BaseBean;

import java.io.Serializable;
import java.util.*;
import java.util.ArrayList;
import java.util.logging.*;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import javax.servlet.http.HttpSession;

@ManagedBean(name = "generatedTargetsBean")
@RequestScoped
public class GeneratedTargetsBean extends BaseBean implements Serializable {

    // *********** Local Variables/Objects ***********
    private static final Logger logger = Logger.getLogger(GeneratedTargetsBean.class.getName());
    private int count;
    private int generatedTargetsID;
    private String excludedClmn;
    private GeneratedTargetsTable selectedGeneratedTargets;
    HttpSession session;
    private GeneratedTargetsDAO generatedTargetsDAO;
    private ArrayList<GeneratedTargetsTable> generatedTargetsList;
    private GeneratedTargetsTable generatedTargetsSrchCrit;
    private GeneratedTargetsTable generatedTargetsUpdate;
    private GeneratedTargetsTable generatedTargetsTable;

    FacesContext context;
    ColToOrderBy colToOrderBy;

    // *********** Constructors ***********
    public GeneratedTargetsBean() {
        logger.info(" Constructor GeneratedTargetsBean()");
        excludedClmn="6";
        context = getContext();
        session = getSession();
        generatedTargetsDAO = new GeneratedTargetsDAO();
        generatedTargetsList = new ArrayList<GeneratedTargetsTable>();
        generatedTargetsTable = new GeneratedTargetsTable();
        generatedTargetsSrchCrit = (GeneratedTargetsTable) session.getAttribute("sessionGeneratedTargetsSrchCrit");
        generatedTargetsUpdate = (GeneratedTargetsTable) session.getAttribute("generatedTargetsUpdateSess");
        colToOrderBy = (ColToOrderBy) session.getAttribute("sessionColToOrderBy");

        if (generatedTargetsSrchCrit == null) {
            generatedTargetsSrchCrit = new GeneratedTargetsTable();
        }

        if (generatedTargetsUpdate == null) {
            generatedTargetsUpdate = new GeneratedTargetsTable();
        }



        fillGeneratedTargetsList();
    }


    // *********** Setters & Getters ***********
    public String getExcludedClmn() {
        return excludedClmn;
    }

    public void setExcludedClmn(String excludedClmn) {
        this.excludedClmn = excludedClmn;
    }

    public GeneratedTargetsTable getGeneratedTargetsUpdate() {
        generatedTargetsUpdate = (GeneratedTargetsTable) session.getAttribute("generatedTargetsUpdateSess");
         if (generatedTargetsUpdate == null) {
            generatedTargetsUpdate = new GeneratedTargetsTable();
        }
        return generatedTargetsUpdate;
    }

    public void setGeneratedTargetsUpdate(GeneratedTargetsTable generatedTargetsUpdate) {
        session.setAttribute("generatedTargetsUpdateSess", generatedTargetsUpdate);
        this.generatedTargetsUpdate = generatedTargetsUpdate;
    }



    public GeneratedTargetsBean(GeneratedTargetsTable generatedTargetsTable) {
        logger.info("Constructor GeneratedTargetsBean( GeneratedTargetsTable generatedTargetsTable )");
        this.generatedTargetsTable = generatedTargetsTable;
    }

    public GeneratedTargetsTable getGeneratedTargetsTable() {
        return generatedTargetsTable;
    }

    public void setGeneratedTargetsTable(GeneratedTargetsTable generatedTargetsTable) {
        this.generatedTargetsTable = generatedTargetsTable;
    }

    public GeneratedTargetsTable getGeneratedTargetsSrchCrit() {
        return generatedTargetsSrchCrit;
    }

    public void setGeneratedTargetsSrchCrit(GeneratedTargetsTable generatedTargetsSrchCrit) {
        this.generatedTargetsSrchCrit = generatedTargetsSrchCrit;
    }

    public GeneratedTargetsTable getSelectedGeneratedTargets() {
        return selectedGeneratedTargets;

    }

    public void setSelectedGeneratedTargets(GeneratedTargetsTable selectedGeneratedTargets) {
        this.selectedGeneratedTargets = selectedGeneratedTargets;
    }

    public ArrayList<GeneratedTargetsTable> getGeneratedTargetsList() {
        return generatedTargetsList;
    }
//******************************
    private ArrayList<SelectItem> mainProjectList = new ArrayList<SelectItem>();
    private MainProjectDAO mainProjectDAO = new MainProjectDAO();
    public ArrayList<SelectItem> getMainProjectList() {
        getAllMainProject();
        return mainProjectList;
    }

    public void setMainProjectList(ArrayList<SelectItem> mainProjectList) {
        this.mainProjectList = mainProjectList;
    }

    public void getAllMainProject() {
        mainProjectList = new ArrayList<SelectItem>();
        MainProjectTable groupTable = new MainProjectTable();
        mainProjectList.add(new SelectItem("", "- Select Role -"));
        try {
            Iterator itMainProject = mainProjectDAO.getByCriterionOrderedBy(null, null).iterator();
            while (itMainProject.hasNext()) {
                groupTable = (MainProjectTable) itMainProject.next();
                mainProjectList.add(new SelectItem(groupTable.getID(), groupTable.getName()));
                groupTable = new MainProjectTable();
            }

        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in public void getAllMainProject() message: {0}Cause: {1}", new Object[]{e.getMessage(), e.getCause()});
        }
    }
    //******************************

    // *********** Actions ***********
    public void doFilter() {
        logger.log(Level.INFO, "Starting: public void doFilter () in GeneratedTargetsBean");
        try {
            generatedTargetsSrchCrit.setFilter(true);
            this.generatedTargetsList = generatedTargetsDAO.getByCriterionOrderedBy(generatedTargetsSrchCrit, null);

            session.setAttribute("sessionGeneratedTargetsSrchCrit", generatedTargetsSrchCrit);
            fillGeneratedTargetsList();
            logger.log(Level.INFO, "Finishing: public void doFilter () in GeneratedTargetsBean");
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in doFilter() message: {0}Cause: {1}", new Object[]{e.getMessage(), e.getCause()});
        }
    }

        public void clearFilter(){
        try {
            generatedTargetsSrchCrit = null;
            session.setAttribute("sessionGeneratedTargetsSrchCrit",generatedTargetsSrchCrit);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void sortByID() {

        logger.log(Level.INFO, "public void fillGeneratedTargetsList ()");

        try {

            if (generatedTargetsSrchCrit != null && colToOrderBy != null) {
                this.generatedTargetsList = generatedTargetsDAO.getByCriterionOrderedBy(generatedTargetsSrchCrit, colToOrderBy);
            } else if (colToOrderBy != null) {
                this.generatedTargetsList = generatedTargetsDAO.getByCriterionOrderedBy(null, colToOrderBy);
            } else if (generatedTargetsSrchCrit != null) {
                this.generatedTargetsList = generatedTargetsDAO.getByCriterionOrderedBy(generatedTargetsSrchCrit, null);
            } else {
                this.generatedTargetsList = generatedTargetsDAO.getByCriterionOrderedBy(null, null);
            }




        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception occured in public void fillGeneratedTargetsList () . Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});
        }
    }

    public void update() {
        try {
            int parID = new Integer(session.getAttribute("generatedTargetsIDSess").toString());
            generatedTargetsUpdate.setID(parID);
            generatedTargetsDAO.updateByID(generatedTargetsUpdate);

        } catch (Exception e) {
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Exception Occure",
                    "Sorry!! an exception occured interface the update action, the webmaster is notified and if action required it will be taken shortly"));
            logger.log(Level.WARNING, "Exception occured in Update(). Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});

        }
    }

    public void Insert() {
        logger.info("public void Insert()");
        try {

            int isInserted = -1;


            isInserted = generatedTargetsDAO.insert(generatedTargetsTable);

            if (isInserted != -1) {
                getContext().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Saved Successfully", "GeneratedTargets Added"));
                fillGeneratedTargetsList();
            } else {
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "error", "error shit"));
                logger.log(Level.WARNING, "Exception occured in Insert(). Error Message: isInserted != -1 Probably exception occure in generatedTargetsDAO.insert( generatedTargetsTable )");

            }

        } catch (Exception e) {

            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Exception Occure",
                    "Sorry!! an exception occured, the webmaster is notified and if action required it will be taken shortly"));
            logger.log(Level.WARNING, "Exception occured in delete(). Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});

        }
    }

    public void delete() {
        try {

            int parID = new Integer(session.getAttribute("generatedTargetsIDSess").toString());



            generatedTargetsDAO.deleteByID(parID);
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "GeneratedTargets Deleted"));
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception occured in delete(). Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Exception Occure", "Sorry!! an exception occured, the webmaster is notified and if action required it will be taken shortly"));
        }
    }

    public void setIDinSession() {
        try {
            Map<String, String> params = context.getExternalContext().getRequestParameterMap();
            int parID = new Integer(params.get("generatedTargetsID"));
            generatedTargetsUpdate = (GeneratedTargetsTable) generatedTargetsDAO.getByID(parID);
            session.setAttribute("generatedTargetsUpdateSess", generatedTargetsUpdate);
            session.setAttribute("generatedTargetsIDSess", parID);
        } catch (Exception ex) {
            Logger.getLogger(GeneratedTargetsBean.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void fillGeneratedTargetsList() {
        logger.log(Level.INFO, "Starting: public void fillGeneratedTargetsList () in GeneratedTargetsBean");
        try {

            if ((generatedTargetsSrchCrit != null && generatedTargetsSrchCrit.isFilter()) && colToOrderBy != null) {
                this.generatedTargetsList = generatedTargetsDAO.getByCriterionOrderedBy(generatedTargetsSrchCrit, colToOrderBy);
            } else if (colToOrderBy != null) {
                this.generatedTargetsList = generatedTargetsDAO.getByCriterionOrderedBy(null, colToOrderBy);
            } else if (generatedTargetsSrchCrit != null && generatedTargetsSrchCrit.isFilter()) {
                this.generatedTargetsList = generatedTargetsDAO.getByCriterionOrderedBy(generatedTargetsSrchCrit, null);
            } else {
                this.generatedTargetsList = generatedTargetsDAO.getByCriterionOrderedBy(null, null);
            }



            logger.log(Level.INFO, "finishing: public void fillGeneratedTargetsList () in GeneratedTargetsBean");
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception occured in public void fillGeneratedTargetsList () . Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});
        }
    }
}

